@extends('admin.base')
@section('title', '商品列表')
@section('content')
<div class="layui-card">
    <fieldset class="layui-elem-field">
        
    </fieldset>
    
    <blockquote class="layui-elem-quote layui-quote-nm">
        {{$attr_type_name->tname}} 包含的规格
    </blockquote>
    <div class="layui-card-body">
        @if(count($yxzlist)>0)
            <form class="layui-form layui-form-pane" method="post" action="{{url('admin/update_xzattr')}}" >
                @csrf
                <input type="hidden" name="tid" value="{{$attr_type_name->id}}" >
                @foreach($yxzlist as $yxzvo)
                    <div class="layui-form-item" pane>
                        <label class="layui-form-label">{{$yxzvo->kname}}</label>
                        <div class="layui-input-inline" style="width: auto;">
                            @foreach($yxzvo->info as $valvo)
                                <input type="checkbox" name="name[]" value="{{$yxzvo->pivot->Kid}}-{{$valvo->id}}" title="{{$valvo->vname}}" lay-skin="primary"  @if($valvo->is_xz == 1) checked @endif>
                            @endforeach
                        </div>
                    </div>
                @endforeach
                <div class="layui-form-item">
                    <button class="layui-btn" lay-submit lay-filter="formDemo_up">修改</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </form>
        @else
            暂未选择规格
        @endif
    </div>
    
    <blockquote class="layui-elem-quote layui-quote-nm">
        未包含的规格
    </blockquote>
	<div class="layui-card-body">
        <form class="layui-form" method="post" action="" >
           @csrf
            <input type="hidden" name="tid" value="{{$attr_type_name->id}}" >
            <div class="layui-form-item" >
                <label class="layui-form-label" style="width:auto;text-align: center;" >
                    <select name="kid" lay-filter="kids" lay-verify="" lay-search >
                        <option value="0">==请选择==</option>
                        <!-- {volist name="wxzlist" id="wxzvo"} -->
                        @foreach($wxzlist as $wxzvo)
                            <option value="{{$wxzvo->id}}">{{$wxzvo->kname}}</option>
                        @endforeach
                    </select>     
                </label>
                <div class="layui-input-inline layui-form" lay-filter="test2" id="valueslist" style="width: auto;">

                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </form>
	</div>
</div>
@endsection
@section('js')
<script type="text/javascript">
    layui.use('form', function(){
        let form = layui.form;
        form.render();
        //监听提交
        //修改
        form.on('submit(formDemo_up)', function(data){        
            layer.confirm('确认执行此操作？', {icon: 3, title:'提示'}, function(index){
                    ajax("{{url('admin/attrtype/update_xzattr')}}",data.field, function(data){
                        layer.close();
                        layer.closeAll();
                        layer.msg(data.msg);
                        if(data.code == 1){
                            window.location.reload();
                        }
                    });
                });
            return false;
        });
        
        form.on('select(kids)', function(data){
            //console.log(data.value); //得到被选中的值
            $.ajax({
                url:"{{url('admin/attrtype/xzattr_value')}}",//请求url
                type:"get",//请求方式
                data:{kid:data.value},//传输参数 
                async:true,//异步请求
                dataType:"json",//返回数据格式
                success:function(data){
                    if(data.code == 1){
                        console.log(data);
                        //return false;
                        $("#valueslist").empty();
                        /*
                        for(let i=0;i<(data['list'].length);i++){
                            let info = "<input type='checkbox' name='name[]' value='"+data['list'][i]['id']+"' title='"+data['list'][i]['vname']+"' lay-skin='primary'>";
                            //console.log(info);
                            $("#valueslist").append(info);
                        }
                        */                      
                        for(let i of data.data){
                            //console.log(i.id);
                            let info = "<input type='checkbox' name='name[]' value='"+i.id+"' title='"+i.vname+"' lay-skin='primary'>";
                            // let info = `<input type='checkbox' name='name[]' value='${i.id}' title='${i.property_name}' lay-skin='primary'>`;//多行使用
                            $("#valueslist").append(info);
                        }
                        
                        form.render('checkbox','test2'); //刷新checkbox复选框（含开关）渲染
                    }else{
                        layer.msg(data['msg']);
                        //alert(data.data);
                    }
                },
                error:function(){
                    alert("服务器正忙！");
                },
            })
            
        });      
        
        //添加
        form.on('submit(formDemo)', function(data){
            //alert('aaa');
            layer.confirm('确认执行此操作？', {icon: 3, title:'提示'}, function(index){
                    ajax("{{url('admin/attrtype/insert_xzattr')}}",data.field, function(data){
                        layer.close();
                        layer.closeAll();
                        layer.msg(data.msg);
                        if(data.code == 1){
                            window.location.reload();
                        }
                    });
                });
            return false;
        });
    });
    
</script>
@endsection